package com.hema.auction.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hema.auction.R;
import com.hema.auction.adapter.SunSquareAdapter;
import com.hema.auction.base.BaseActivity;
import com.hema.auction.bean.SunSquareInfo;
import com.hema.auction.http.HttpTag;
import com.hema.auction.http.biz.HttpManager;
import com.hema.auction.utils.Constant;
import com.hema.auction.widget.view.EmptyView;
import com.hema.auction.widget.view.MyLoadMoreView;
import com.hema.auction.widget.view.RefreshRecyclerView;
import com.hema.auction.widget.view.TitleView;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * 描述:晒单列表
 * 作者:john
 */
public class SunSquareActivity extends BaseActivity {

    @BindView(R.id.title_view)
    TitleView titleView;
    @BindView(R.id.refresh_recycler_view)
    RefreshRecyclerView refreshRecyclerView;
    private SunSquareAdapter adapter;
    private List<SunSquareInfo> infos = new ArrayList<>();
    private boolean isMyShow;
    private EmptyView emptyView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sun_square);
        ButterKnife.bind(this);
        isMyShow = getIntent().getBooleanExtra(Constant.EXTRA_MY_SUN, false);
        if (isMyShow) {
            titleView.getRightImageVIew().setVisibility(View.GONE);
            titleView.setTitle(R.string.my_sun);
        }
        titleView.getRightImageVIew().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putBoolean(Constant.EXTRA_MY_SUN, true);
                intentTo(SunSquareActivity.class, bundle);
            }
        });
        init();
    }

    private void init() {
        refreshRecyclerView.setVerLayoutManager();
        adapter = new SunSquareAdapter(infos);
        adapter.setLoadMoreView(new MyLoadMoreView());
        emptyView = new EmptyView(this);
        emptyView.setViewType(EmptyView.SUN);
        adapter.setEmptyView(emptyView);
        refreshRecyclerView.setAdapter(adapter);
        refreshRecyclerView.setOnRefreshListener(this);
        refreshRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
                .size(2).showLastDivider().color(getResources().getColor(R.color.colorDivider)).build());
        adapter.setOnLoadMoreListener(this, refreshRecyclerView.getRecyclerView());
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putParcelable(Constant.EXTRA_SUN_INFO, (SunSquareInfo) adapter.getItem(position));
                intentTo(SunDetailActivity.class, bundle);
            }
        });
        refreshRecyclerView.setRefresh(true);
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        pageIndex = 1;
        if (isMyShow)
            HttpManager.getInstance(this).getMyShows(pageIndex, pageSize, this);
        else
            HttpManager.getInstance(this).getShows(pageIndex, pageSize, this);
    }

    @Override
    protected void onLoadMore() {
        super.onLoadMore();
        pageIndex++;
        if (isMyShow)
            HttpManager.getInstance(this).getMyShows(pageIndex, pageSize, this);
        else
            HttpManager.getInstance(this).getShows(pageIndex, pageSize, this);
    }

    @Override
    protected void onSuccess(HttpTag httpTag, Call call, JSONObject result) {
        super.onSuccess(httpTag, call, result);
        switch (httpTag) {
            case TAG_GET_SHOWS:
            case TAG_GET_MY_SHOWS:
                try {
                    if (result.getInt("errcode") == 0) {
                        List<SunSquareInfo> sunSquareInfos = new Gson().fromJson(result.getJSONArray("data").toString(), new TypeToken<List<SunSquareInfo>>() {
                        }.getType());
                        if (sunSquareInfos != null) {
                            if (pageIndex == 1) {
                                adapter.setNewData(sunSquareInfos);
                                if (sunSquareInfos.size() < pageSize) {
                                    adapter.setEnableLoadMore(false);
                                } else {
                                    adapter.setEnableLoadMore(true);
                                }
                            } else {
                                adapter.addData(sunSquareInfos);
                                adapter.loadMoreComplete();
                            }

                            if (sunSquareInfos.size() < pageSize) {
                                adapter.loadMoreEnd();
                            }
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    @Override
    protected void onFinish(HttpTag httpTag) {
        super.onFinish(httpTag);
        refreshRecyclerView.setRefresh(false);
    }
}
